home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AOCE Sample Code / PowerTalk Access Modules / Sample SMSAM / SampleSMSAM Source / BuildingBlocks / BufferTests.cp < prev    next >
Encoding:
Text File  |  1995-07-28  |  5.0 KB  |  205 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        BufferTests.cp
  3.  
  4.     Copyright:    © 1991-1994 by Apple Computer, Inc.
  5.                 All rights reserved.
  6.  
  7.     Part of the AOCE Sample SMSAM Package.  Consult the license
  8.     which came with this software for your specific legal rights.
  9.  
  10. */
  11.  
  12.  
  13.  
  14. #if debug
  15.  
  16. #ifndef    __TUPLETESTS__
  17. #include "TupleTests.h"
  18. #endif
  19.  
  20. #ifndef    __STDLIB__
  21. #include "StdLib.h"
  22. #endif
  23.  
  24. #ifndef    __MEMORY__
  25. #include "Memory.h"
  26. #endif
  27.  
  28. #ifndef    __BUFFER__
  29. #include "Buffer.h"
  30. #endif
  31.  
  32. #pragma segment BufferTests
  33.  
  34. /***********************************|****************************************/
  35.  
  36. Boolean 
  37. SetLengthTest ( ABuffer& buffer, unsigned long trials = 100, unsigned long maxSize = 1024 )
  38. {
  39.     TEST_REPORT(SetLengthTest ( ABuffer&, unsigned long, unsigned long ));
  40.     
  41.     maxSize++;
  42.     
  43.     while ( trials-- > 0 )
  44.     {
  45.         unsigned long requestedLength = (unsigned long) rand () % maxSize;
  46.         unsigned long actualLength = buffer.SetPhysicalLength ( requestedLength );
  47.         
  48.         if ( !ASSERT ( actualLength == requestedLength ) )
  49.         {
  50.             chris << "buffer.SetLength ( " << requestedLength << " ) -> " << actualLength << '\n';
  51.             chris << buffer << '\n';
  52.             return false;
  53.         }
  54.     }
  55.     
  56.     return true;
  57. }
  58.  
  59. /***********************************|****************************************/
  60.  
  61. Boolean
  62. InvariantTest ( const ABuffer& buffer )
  63. {
  64.     TEST_REPORT(InvariantTest ( const ABuffer& buffer ));
  65.     ASSERT_RETURN_ZERO ( buffer == buffer );
  66.     ASSERT_RETURN_ZERO ( !( buffer != buffer ) );
  67.     ASSERT_RETURN_ZERO ( buffer.GetPhysicalStart () == (const void*) buffer );
  68.     return true;
  69. }
  70.  
  71. /***********************************|****************************************/
  72.  
  73. Boolean
  74. EqualityTest ( const ABuffer& a, const void* b, unsigned long c )
  75. {
  76.     TEST_REPORT(EqualityTest ( const ABuffer& a, const void* b, unsigned long c ));
  77.     ASSERT_RETURN_ZERO ( a.GetPhysicalLength () == c );
  78.     ASSERT_RETURN_ZERO ( memcmp ( a.GetPhysicalStart (), b, (unsigned int) c ) == 0 );
  79.     return true;
  80. }
  81.  
  82. /***********************************|****************************************/
  83.  
  84. Boolean
  85. EqualityTest ( const ABuffer& a, const ABuffer& b )
  86. {
  87.     TEST_REPORT(EqualityTest ( const ABuffer& a, const ABuffer& b ));
  88.     ASSERT_RETURN_ZERO ( a.GetPhysicalLength () == b.GetPhysicalLength () );
  89.     ASSERT_RETURN_ZERO ( memcmp ( a.GetPhysicalStart (), b.GetPhysicalStart (), (unsigned int) a.GetPhysicalLength () ) == 0 );
  90.     ASSERT_RETURN_ZERO ( a == b );
  91.     ASSERT_RETURN_ZERO ( b == a );
  92.     ASSERT_RETURN_ZERO ( !( a != b ) );
  93.     ASSERT_RETURN_ZERO ( !( b != a ) );
  94.     return true;
  95. }
  96.  
  97. /***********************************|****************************************/
  98.  
  99. Boolean
  100. AssignConstTest ( const ABuffer& a )
  101. {
  102.     TEST_REPORT(AssignConstTest ( const ABuffer& a ));
  103.     CBuffer b ( a );
  104.     ASSERT_RETURN_ZERO ( EqualityTest ( a, b ) );
  105.     CBuffer c = a;
  106.     ASSERT_RETURN_ZERO ( EqualityTest ( a, c ) );
  107.     return true;
  108. }
  109.  
  110. /***********************************|****************************************/
  111.  
  112. Boolean
  113. AssignTest ( ABuffer& a )
  114. {
  115.     TEST_REPORT(AssignTest ( ABuffer& a ));
  116.     ASSERT_RETURN_ZERO ( AssignConstTest ( a ) );
  117.     return true;
  118. }
  119.  
  120. /***********************************|****************************************/
  121.  
  122. Boolean 
  123. TestSuite ( ABuffer& buffer )
  124. {
  125.     TEST_REPORT(TestSuite ( ABuffer& buffer ));
  126.     ASSERT_RETURN_ZERO ( InvariantTest ( buffer ) );
  127.     ASSERT_RETURN_ZERO ( SetLengthTest ( buffer ) );
  128.     ASSERT_RETURN_ZERO ( AssignTest ( buffer ) );
  129.     ASSERT_RETURN_ZERO ( InvariantTest ( buffer ) );
  130.     return true;
  131. }
  132.  
  133. /***********************************|****************************************/
  134.  
  135. Boolean
  136. CBufferTests ()
  137. {
  138.     TEST_REPORT(CBufferTests);
  139.     
  140.     const unsigned long kSmallSize = 1;
  141.     const unsigned long kBigSize = 256;
  142.     
  143.     {
  144.     // CBuffer::CBuffer ( unsigned long length )
  145.         {
  146.             CBuffer buffer ( kBigSize );
  147.             ASSERT_RETURN_ZERO ( buffer.GetPhysicalLength () == kBigSize );
  148.             ASSERT_RETURN_ZERO ( TestSuite ( buffer ) );
  149.         }
  150.         
  151.         {
  152.             CBuffer buffer ( kSmallSize );
  153.             ASSERT_RETURN_ZERO ( buffer.GetPhysicalLength () == kSmallSize );
  154.             ASSERT_RETURN_ZERO ( TestSuite ( buffer ) );
  155.         }
  156.     }
  157.     
  158.     {
  159.     // CBuffer::CBuffer ( const void* source, unsigned long length )
  160.         {
  161.             char source [ kBigSize ];
  162.             CBuffer buffer ( source, kBigSize );
  163.             ASSERT_RETURN_ZERO ( EqualityTest ( buffer, source, kBigSize ) );
  164.             ASSERT_RETURN_ZERO ( TestSuite ( buffer ) );
  165.         }
  166.         {
  167.             char source [ kSmallSize ];
  168.             CBuffer buffer ( source, kSmallSize );
  169.             ASSERT_RETURN_ZERO ( EqualityTest ( buffer, source, kSmallSize ) );
  170.             ASSERT_RETURN_ZERO ( TestSuite ( buffer ) );
  171.         }
  172.     }
  173.     
  174.     {
  175.     // CBuffer::CBuffer ( const ABuffer& )
  176.         {
  177.             const CBuffer source ( kBigSize );
  178.             CBuffer buffer ( source );
  179.             ASSERT_RETURN_ZERO ( EqualityTest ( buffer, source ) );
  180.             ASSERT_RETURN_ZERO ( TestSuite ( buffer ) );
  181.         }
  182.         {
  183.             const CBuffer source ( kSmallSize );
  184.             CBuffer buffer ( source );
  185.             ASSERT_RETURN_ZERO ( EqualityTest ( buffer, source ) );
  186.             ASSERT_RETURN_ZERO ( TestSuite ( buffer ) );
  187.         }
  188.     }
  189.     
  190.     return true;
  191. }
  192.  
  193. /***********************************|****************************************/
  194.  
  195. void BufferTests ()
  196. {
  197.     CBufferTests ();
  198. //     CPrefixBufferTests ();
  199. //     CWrapperBufferTests ();
  200. }
  201.  
  202. /***********************************|****************************************/
  203.  
  204. #endif    // debug
  205.